<?php
class Con_ttd extends CI_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->load->library('auth');
		$this->load->model('ttdmodel');
		$this->load->model('usermodel');
	}
	public function list_ttd()
	{
		$this->auth->restrict();
		$data['menu'] = $this->usermodel->get_menu();
		$data['all_ttd'] = $this->ttdmodel->get_all_ttd();
		$this->template->set('title','Daftar Penanda Tangan | Aplikasi Persuratan');
		$this->template->load('template','list_ttd',$data);
	}
	public function tambah()
	{
		$this->auth->restrict();
		$this->load->library('form_validation');
		$this->form_validation->set_rules('nama','nama','trim|required');
		$this->form_validation->set_rules('NIP','NIP','trim|required|numeric|max_length[18]');
		$this->form_validation->set_rules('jabatan','jabatan','trim|required');
		
		$this->form_validation->set_error_delimiters('<span style="color:#FF0000">','</span>');
		
		if ($this->form_validation->run() == FALSE)
		{
			$data['menu'] = $this->usermodel->get_menu();
			$this->template->set('title','Tambah Penanda Tangan | Aplikasi Persuratan');
			$this->template->load('template','form_tambah_ttd',$data);
		}
		else
		{
			$data_ttd = array (
			'nama' =>$this->input->post('nama'),
			'NIP' =>$this->input->post('NIP'),
			'jabatan' =>$this->input->post('jabatan')
			);
			$this->ttdmodel->tambah_data_ttd($data_ttd);
			//kembali ke halaman list penanda tangan
			redirect ('con_ttd/list_ttd');
		}
	}
	public function edit()
	{
		//mencegah user yang belum login untuk mengakses halaman ini
		$this->auth->restrict();
		
		$this->load->library('form_validation');
		
		$this->form_validation->set_rules('nama','nama','trim|required');
		$this->form_validation->set_rules('nip','nip','trim|required|numeric|max_length[18]');
		$this->form_validation->set_rules('jabatan','jabatan','trim|required');
	
		
		$this->form_validation->set_error_delimiters('<span style="color:#FF0000">','</span>');
		
		$id = $this->uri->segment(3);
		
		if ($this->form_validation->run() == FALSE)
		{
			//$data['level'] = $this->usermodel->get_all_level();
			//dapatkan user yang akan diedit
			$data['menu'] = $this->usermodel->get_menu();
			$data['ttd'] = $this->ttdmodel->get_ttd_by_id($id);
			$this->template->set('title','Edit Penanda Tangan | Aplikasi Persuratan');
			$this->template->load('template','form_edit_ttd',$data);
		}
		else
		{
			$data_ttd = array (
			'nama' =>$this->input->post('nama'),
			'nip' =>$this->input->post('nip'),
			'jabatan' =>$this->input->post('jabatan')
			);
			$this->ttdmodel->edit_data_ttd($data_ttd,$id);
			//kembali ke halaman manajemen user
			redirect ('con_ttd/list_ttd');
		}
	}
	public function hapus()
	{
		//mencegah user yang belum login untuk mengakses halaman ini
		$this->auth->restrict();
		
		//dapatkan id user dari segment ke-3 dari URI
		$id = $this->uri->segment(3);
		$this->ttdmodel->delete_ttd($id);
		//kembali ke halaman list ttd
		redirect ('con_ttd/list_ttd');
	}
		
}